package it.tukano.odt;

import it.tukano.fun.Tuple2;
import it.tukano.lang.StringExt;
import it.tukano.swingdeco.containers.JPanelExt;
import it.tukano.swingdeco.text.JTextFieldExt;
import java.awt.GridLayout;
import javax.swing.BorderFactory;

public class OdtIntegerEditor extends OdtFieldEditor {

    /* default serial version uid*/
    private static final long serialVersionUID = 0;
    private final JTextFieldExt field;
    private final String defaultValue;

    public OdtIntegerEditor(Integer size, String label) {
        this(size, label, new StringExt(""), false);
    }

    public OdtIntegerEditor(Integer size, String label, StringExt defaultValue, boolean useScript) {
        this.defaultValue = defaultValue.getAsBigInteger("");
        setBorder(BorderFactory.createTitledBorder(label));
        this.field = new JTextFieldExt(size);
        add(new JPanelExt(new GridLayout(1, 1)).append(field));
        if(useScript) addSideComponent(new ScriptEvaluator(this).getRunButton());
    }

    @Override
    public void setValue(StringExt t) {
        field.setText(t.toString());
    }

    @Override
    public Tuple2<StringExt, String> applyEditorValue(OdtField odtField, String xml) {
        return Tuple2.newInstance(new StringExt(field.getText()), xml);
    }

    @Override
    public void clearValue() {
        field.setText(defaultValue);
    }

}
